package com.mdz.sbgl.Controller;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Entity.*;
import com.mdz.sbgl.Mapper.YgxxbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/ygxxb")
public class YgxxbController {
    @Autowired
    private YgxxbMapper ygxxbMapper;

    @Autowired
    private HttpSession session;

    @ResponseBody
    @RequestMapping(value = "/getbm/{gsid}",method = RequestMethod.GET)
    public List<Ygxxb> getbm(@PathVariable("gsid") String gsid){
        return ygxxbMapper.getbm(gsid);
    }

    @ResponseBody
    @RequestMapping(value = "/getygxx/{bm}/{gsid}",method = RequestMethod.GET)
    public List<Ygxxb> getygxx(@PathVariable("bm") String bm,@PathVariable("gsid") String gsid){
        List<Ygxxb> ygxxbs=ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("bm",bm).eq("gsid",gsid));
        return ygxxbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getygxxb/{gsid}",method = RequestMethod.GET)
    public List<Ygxxb> getygxxb(@PathVariable("gsid") String gsid){
        List<Ygxxb> ygxxbs=ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("gsid",gsid));
        return ygxxbs;
    }

    @ResponseBody
    @RequestMapping(value = "/getbmmc/{bmid}", method = RequestMethod.GET)
    public String getbmmc(@PathVariable(value = "bmid",required = false) String bmid){
        try {
            List<Ygxxb> ygxxbs=ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("bm",bmid));
            return ygxxbs.get(0).getBm();
        } catch (Exception e) {
            return "无查询记录";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getbmbyshiyrandbm/{shiyr}/{bm}/{gsid}", method = RequestMethod.GET)
    public String getbmbyshiyrandbm(@PathVariable(value = "shiyr",required = false) String shiyr,@PathVariable(value = "bm",required = false) String bm,@PathVariable(value = "gsid",required = false) String gsid){
        List<Ygxxb> ygxxbs= null;
        try {
            ygxxbs = ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("shiyr",shiyr).eq("bm",bm).eq("gsid",gsid));
            return ygxxbs.get(0).getBm();
        } catch (Exception e) {
            return "全公司上下查无此人";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getbmidbyshiyr/{shiyr}", method = RequestMethod.GET)
    public String getbmidbyshiyr(@PathVariable(value = "shiyr",required = false) String shiyr){
        List<Ygxxb> ygxxbs= null;
        try {
            ygxxbs = ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("shiyr",shiyr));
            return ygxxbs.get(0).getBm();
        } catch (Exception e) {
            return "全公司上下查无此人";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateygxxb/{shiyr}", method = RequestMethod.PUT)
    public Integer updatesbxxb(Ygxxb ygxxb, @PathVariable("shiyr") String shiyr) {
        return ygxxbMapper.update(ygxxb, new UpdateWrapper<Ygxxb>().eq("shiyr", ygxxb.getShiyr()));
    }

    @ResponseBody
    @RequestMapping(value = "/updateygxxbbyid/{ids}", method = RequestMethod.PUT)
    public Integer updateygxxbbyid(Ygxxb ygxxb, @PathVariable("ids") String ids) {
        String zjm = ygxxbMapper.getzjm(ygxxb.getShiyr());
        ygxxb.setZjm(zjm);
        return ygxxbMapper.update(ygxxb, new UpdateWrapper<Ygxxb>().eq("id", ids));
    }

    @ResponseBody
    @RequestMapping(value = "/getygxxbbygblu/{gsid}/{bm}/{shiyr}/{lgname}", method = RequestMethod.GET)
    public List<Ygxxb> getygxxbbygblu(@PathVariable("gsid") String gsid,@PathVariable("bm") String bm,@PathVariable("shiyr") String shiyr,@PathVariable("lgname") String lgname){
        return ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("gsid",gsid).eq("bm",bm).eq("shiyr",shiyr).eq("login_name",lgname));
    }

    @ResponseBody
    @RequestMapping(value = "/updateygxxbbygsid/{gsid01}/{bm01}", method = RequestMethod.PUT)
    public Integer updateygxxbbygsid(Ygxxb ygxxb, @PathVariable("gsid01") String gsid01,@PathVariable("bm01") String bm01) {
        return ygxxbMapper.update(ygxxb, new UpdateWrapper<Ygxxb>().eq("gsid", gsid01).eq("bm",bm01));
    }

    @ResponseBody
    @RequestMapping(value = "/updatepassword/{uId}", method = RequestMethod.PUT)
    public Integer updatepassword(Ygxxb ygxxb, @PathVariable("uId") String uId) {
        return ygxxbMapper.update(ygxxb, new UpdateWrapper<Ygxxb>().eq("id",uId));
    }

    @ResponseBody
    @RequestMapping(value = "/insertyg", method = RequestMethod.POST)
    public int insertyg(Ygxxb ygxxb) {
        String zjm = ygxxbMapper.getzjm(ygxxb.getShiyr());
        String gsid=ygxxb.getGsid();
        ygxxb.setZjm(zjm);
        ygxxb.setGsid(gsid);
        String lgname="";
        LocalDate today = LocalDate.now();
        LocalTime time=LocalTime.now();
        String Password = SecureUtil.md5(ygxxb.getLoginPassword());
        ygxxb.setLoginPassword(Password);
        if (ygxxb.getIsgly()==null){
            ygxxb.setIsgly("否");
            lgname=zjm+" "+today+" "+time;
            ygxxb.setLoginName(lgname);
        }else if (ygxxb.getIsgly()==""){
            ygxxb.setIsgly("否");
            lgname=zjm+" "+today+" "+time;
            ygxxb.setLoginName(lgname);
        }else {
            ygxxb.setIsgly("是");
            ygxxb.setLoginName(ygxxb.getLoginName());
        }
        return ygxxbMapper.insert(ygxxb);
    }

    @ResponseBody
    @RequestMapping(value = "/getygxxblist",method = RequestMethod.GET)
    public String getygxxbflblist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, @RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj,@RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
//        List<Ygxxb> ncList=ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().like("gsid",gsid).and(wrapper -> wrapper.like("bm",cxtj).or().like("shiyr",cxtj).or().like("zjm",cxtj)));
        String userid=session.getAttribute("userid").toString();
        List<Ygxxb> ncList=new ArrayList<>();
        if (userid.equals("1")){
            ncList=ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().ne("bm","admin").and(wrapper -> wrapper.like("gsid",cxtj).or().like("bm",cxtj).or().like("zjm",cxtj).or().like("shiyr",cxtj).or().like("login_name",cxtj)));
        }else {
            System.out.println(userid);
            ncList=ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("gsid",gsid).and(wrapper -> wrapper.like("bm",cxtj).or().like("shiyr",cxtj).or().like("zjm",cxtj)));
        }
        PageInfo page=new PageInfo(ncList);
        List<Ygxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public Integer remove(@PathVariable("id") String id)
    {
        return ygxxbMapper.delete(new QueryWrapper<Ygxxb>().eq("id",id));
    }

    @ResponseBody
    @GetMapping(value = "/login/{gsid}/{loginName}/{loginPassword}")
    public List<Ygxxb> login(@PathVariable("gsid") String gsid, @PathVariable("loginName") String loginName, @PathVariable("loginPassword") String loginPassword) {
        String Password = SecureUtil.md5(loginPassword);
        List<Ygxxb> user = ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("login_name", loginName).eq("login_password", Password).eq("gsid",gsid));
        session.setAttribute("username", user.get(0).getShiyr());
        session.setAttribute("loginname", user.get(0).getLoginName());
        session.setAttribute("userid", user.get(0).getId());
        if (user.get(0).getLoginName().contains("administrator")){
            session.setAttribute("gsid", "");
        }else {
            System.out.println(user.get(0).getGsid());
            session.setAttribute("gsid", user.get(0).getGsid());
        }
        return user;
    }

    @ResponseBody
    @GetMapping(value = "/ksdlbh/{gsid}")
    public List<Ygxxb> ksdlbh(@PathVariable("gsid") String gsid) {
        List<Ygxxb> user = ygxxbMapper.selectList(new QueryWrapper<Ygxxb>().eq("ksdlbh",gsid).eq("isgly","是"));
        if (user.size()>0){
            session.setAttribute("username", user.get(0).getShiyr());
            session.setAttribute("loginname", user.get(0).getLoginName());
            session.setAttribute("userid", user.get(0).getId());
            if (user.get(0).getLoginName().contains("administrator")){
                session.setAttribute("gsid", "");
            }else {
                System.out.println(user.get(0).getGsid());
                session.setAttribute("gsid", user.get(0).getGsid());
            }
            return user;
        }else {
            return null;
        }
    }

    @ResponseBody
    @GetMapping(value = "/getksdlbhcount/{ksdlbh}")
    public String getksdlbhcount(@PathVariable("ksdlbh") String ksdlbh) {
        Integer ksdlbhcount = ygxxbMapper.selectCount(new QueryWrapper<Ygxxb>().eq("ksdlbh",ksdlbh).eq("isgly","是"));
        if (ksdlbhcount==1){
            return "你输入的快速登录编号已存在，快速登录编号不能重复";
        }else {
            return "你输入的快速登录编号不存在，可以正常使用";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateksdlbh/{uId}", method = RequestMethod.PUT)
    public Integer updateksdlbh(Ygxxb ygxxb, @PathVariable("uId") String uId) {
        System.out.println(ygxxb);
        return ygxxbMapper.update(ygxxb, new UpdateWrapper<Ygxxb>().eq("id",uId));
    }

}